//
//  FollowTableViewCell.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/10/31.
//

#import "FollowTableViewCell.h"

@interface FollowTableViewCell ()

@property (nonatomic, strong) UIImageView *avatarImageV;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIImageView *topImageV;
@property (nonatomic, strong) UIButton *topBtn;
@property (nonatomic, strong) UIButton *followBtn;
@property (nonatomic, strong) UILabel *countLabel;

@end
@implementation FollowTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    [self.contentView addSubview:self.avatarImageV];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.topImageV];
    [self.contentView addSubview:self.countLabel];
    [self.contentView addSubview:self.followBtn];
    [self.contentView addSubview:self.topBtn];
    
    [self.avatarImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(50);
        make.centerY.mas_equalTo(0);
        make.leading.mas_equalTo(16);
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.avatarImageV.mas_trailing).offset(8);
        make.top.mas_equalTo(self.avatarImageV.mas_top).offset(4);
    }];
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.nameLabel);
        make.bottom.mas_equalTo(self.avatarImageV.mas_bottom).offset(-4);
    }];
    [self.topImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.nameLabel.mas_trailing).offset(4);
        make.centerY.equalTo(self.nameLabel);
        make.width.height.mas_equalTo(14);
    }];
    [self.followBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-16);
        make.size.mas_equalTo(CGSizeMake(64, 24));
        make.centerY.mas_equalTo(0);
    }];
    [self.topBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.followBtn.mas_leading).offset(-10);
        make.size.mas_equalTo(CGSizeMake(64, 24));
        make.centerY.mas_equalTo(0);
    }];
}

- (void)setModel:(UserModel *)model {
    _model = model;
    [self.avatarImageV sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[UIImage imageNamed:@"avatar"]];
    self.nameLabel.text = model.nickName;
    self.topImageV.hidden = !model.top;
    
    self.topBtn.backgroundColor = model.top ? [UIColor whiteColor] : kBgColor;
    self.topBtn.selected = model.top;
    
    self.followBtn.backgroundColor = model.follow ? kRGB(0xF2F2F2) : kRGB(0xFF6081);
    self.followBtn.selected = model.follow;
    
    NSString *day = [NSString stringWithFormat:@"%ld条",model.dayGoodsCount];
    NSString *all = [NSString stringWithFormat:@"%ld条",model.allGoodsCount];
    NSString *str = [NSString stringWithFormat:@"今日上新 %@ 共计 %@",day,all];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:str];
    
    NSRange range1 = [str rangeOfString:day];
    NSRange range2 = [str rangeOfString:all];
    [attr addAttributes:@{NSForegroundColorAttributeName:kRGB(0xF15757)} range:range1];
    [attr addAttributes:@{NSForegroundColorAttributeName:kRGB(0xF15757)} range:range2];
    self.countLabel.attributedText = attr;
}

- (UIImageView *)avatarImageV {
    if (!_avatarImageV) {
        _avatarImageV = [[UIImageView alloc] init];
        _avatarImageV.contentMode = UIViewContentModeScaleAspectFill;
        _avatarImageV.clipsToBounds = YES;
        _avatarImageV.layer.cornerRadius = 25;
    }
    return _avatarImageV;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = kRGB(0x333333);
        _nameLabel.font = kFontMediium(16);
        [_nameLabel sizeToFit];
    }
    return _nameLabel;
}

- (UIImageView *)topImageV {
    if (!_topImageV) {
        _topImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shopTop"]];
    }
    return _topImageV;
}

- (UILabel *)countLabel {
    if (!_countLabel) {
        _countLabel = [[UILabel alloc] init];
        _countLabel.textColor = kRGB(0x999999);
        _countLabel.font = kFontRegula(12);
        [_countLabel sizeToFit];
    }
    return _countLabel;
}

- (UIButton *)followBtn {
    if (!_followBtn) {
        _followBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _followBtn.backgroundColor = kRGB(0xFF6081);
        [_followBtn setTitle:@"关注" forState:UIControlStateNormal];
        [_followBtn setTitle:@"已关注" forState:UIControlStateSelected];
        [_followBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_followBtn setTitleColor:kRGB(0x333333) forState:UIControlStateSelected];
        _followBtn.layer.cornerRadius = 12;
        _followBtn.layer.masksToBounds = YES;
        _followBtn.titleLabel.font = kFontMediium(12);
        @weakify(self)
        [[_followBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.followBtnBlock) {
                self.followBtnBlock(self.model);
            }
        }];
    }
    return _followBtn;
}

- (UIButton *)topBtn {
    if (!_topBtn) {
        _topBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _topBtn.backgroundColor = kBgColor;
        [_topBtn setTitle:@"置顶" forState:UIControlStateNormal];
        [_topBtn setTitle:@"取消置顶" forState:UIControlStateSelected];
        [_topBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_topBtn setTitleColor:kBgColor forState:UIControlStateSelected];
        _topBtn.titleLabel.font = kFontMediium(12);
        _topBtn.layer.cornerRadius = 12;
        _topBtn.layer.borderColor = kBgColor.CGColor;
        _topBtn.layer.borderWidth = 1;
        _topBtn.layer.masksToBounds = YES;
        @weakify(self)
        [[_topBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.topBtnBlock) {
                self.topBtnBlock(self.model);
            }
        }];
    }
    return _topBtn;
}

@end
